// pages/component/demandhall/demandhall.js
const http = require('../../utils/http')
var Api = require("../../common/api/api.js")
var loadMoreView, page

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //------------------------------------
    feed: [],
    feed_length: 0,
    userId: 0, //当前用户ID

    // banner轮播组件属性
    indicatorDots: true, // 是否显示面板指示点	
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔
    duration: 800, // 滑动动画时长
    scrollTop: 0,
    pageCarousels: {}, //图片轮播

    system: {}, //系统信息
    items: [],
    resultData: [], // 存放数据
    demandNum: 0, //累计解决的需求数
    selectedView: 'article',
    page: 0,

    thumbUpButtonFalse: "../../../image/icon/dianzan.png",
    thumbUpButtonTrue: "../../../image/icon/dianzan_default.png",

    //登录用户openid
    openid: '',
  },

  /**
   * 点击分享
   */
  onShareAppMessage: function(e) {
    let _this = this;

    var id = e.target.dataset.id;
    var content = e.target.dataset.content;
    var res = e.target.dataset.res;

    //调增加分享次数接口
    var data = {
      id: id
    }
    var params = JSON.stringify(data)
    Api.addShare(params).then(function (result) {
      _this.onPullDownRefresh();
    }).catch(function (error) {
      console.log(error)
    })


    //默认分享图
    var imageUrl = '../../../image/icon/2.jpg';
    if(res != ''){
      imageUrl = res[0].path;
    }

    return {
      title: content.substring(0, 10) + "...",
      desc: '未来森林',
      path: '/pages/component/demandhall/demandhall', // 路径，传递参数到指定页面。
      imageUrl: imageUrl,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    page = 0;
    loadMoreView = this.selectComponent("#loadMoreView");
    this.loadData('article', true);
  },

  loadData: function(viewType, showLoading) {
    console.log("加载。。。。。");

    var that = this;
    http.post({
      url: Api.host + '/api/comm/demandHall',
      data: {
        page: page,
        limit: 20,
        openid: that.data.openid,
      },
      showLoading: showLoading,
      success: (res) => {
        var items = that.data.resultData;
        if (page == 0) {
          items = res.demandHalls;
          wx.stopPullDownRefresh()
        } else {
          items = items.concat(res.demandHalls);//res.demandHalls
        }
        that.setData({
          items: items,
          resultData: items,
          selectedView: viewType,
          demandNum: res.demandNum,
        })

        loadMoreView.loadMoreComplete(res.demandHalls)
      },
      fail: () => {
        if (page != 0) {
          loadMoreView.loadMoreFail()
        }
      }
    })
  },
  loadMoreListener: function(e) {
    page += 1
    this.loadData(this.data.selectedView, false)
  },
  clickLoadMore: function(e) {
    this.loadData(this.data.selectedView, false)
  },



  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    page = 0
    this.loadData(this.data.selectedView, false)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    loadMoreView.loadMore()
  },

  onPageScroll: function(e) {
    console.log(e)
    if (e.scrollTop >= this.data.system.windowWidth / 9 * 5 && !this.data.float) {
      this.setData({
        float: true
      })
    } else if (e.scrollTop < this.data.system.windowWidth / 9 * 5 && this.data.float) {
      this.setData({
        float: false
      })
    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      openid: wx.getStorageSync('openid')
    });
  },

  toHomePage:function(e){
    var id = e.currentTarget.dataset.id;
    var openid = this.data.openid;
    console.log("用户登录后的openid：", openid);
    if(openid == null || openid ==""){
      //跳转到登录页面
      wx.navigateTo({
        url: "/pages/component/login/login"
      });
    }else{
      wx.navigateTo({
        url: '../homepage/homepage?id=' + id,
      })
    }
  }

})